﻿// Wrap the last element of each line in CodeRay boxes
// This needs to be in a function because it is run multiple times
// in other places.
// Ludwig: Did not convert this to use jquery completely because
//         I don't really know what it's purpose is, and thus can't
//         really test it.
var wrapLastCodeRayLineElement = function () {
    var CodeRays = $(".CodeRay");
    for (var i = 0, codeLen = CodeRays.length; i < codeLen; i++) {
        var lineNumbers = $("span.no", CodeRays[i]);
        for (var j = 1, lineLen = lineNumbers.length; j < lineLen; j++) {
            var lineNumber = lineNumbers[j],
          prevElt = lineNumber.previousSibling,
          lastEltInLine = document.createElement("span");
            lastEltInLine.className = "lastEltInLine";
            lastEltInLine.appendChild(prevElt);
            lineNumber.parentNode.insertBefore(lastEltInLine, lineNumber);
        }
    }
}

$(function () {
    var content = $("#content");
    var navigation = $("#doc nav");

    // Make sure the main content is bigger than the navigation sidebar
    var lastNavigationLink = $("li.last", navigation);
    var navigationHeight = lastNavigationLink.offset().top + lastNavigationLink.outerHeight() - 45;

    if (navigationHeight > content.outerHeight()) {
        content.css("min-height", navigationHeight);
    }

    // Set the active style on click, so we don't have to wait for the page to reload.
    var selectedClass = "selected";
    var unselectedClass = "unselected";

    // Select all unselected, except for the API reference link
    $(".unselected a", navigation).each(function () {
        var el = $(this);

        el.click(function (event) {
            $("." + selectedClass, navigation)
        .removeClass(selectedClass)
        .addClass(unselectedClass);

            el.parent()
        .removeClass(unselectedClass)
        .addClass(selectedClass);
        });
    });
    $(".unselected a:first", navigation).click();
    // Add gradient on the right if the content overflows
    var CodeRays = $(".CodeRay");
    var codeExamples = $(".code-example");

    for (var i = 0, codeLen = CodeRays.length; i < codeLen; i++) {
        if (CodeRays[i].offsetWidth > 670) {
            $(codeExamples[i]).addClass("code-overflow");
            if ($("nav", codeExamples[i]).length) {
                $(codeExamples[i]).addClass("multiple-lang");
            }
        }
    }

    // Add deep-links to titles
    $("h1, h2, h3, h4, h5, h6", "section#content").each(function () {
        var el = $(this);
        var id = el.attr("id");

        // We have an id and we're not wrapped in a <header>
        if (id && el.parents("header").length === 0 && el.parents("footer").length === 0) {
            var link = $('<a />');
            link.attr("href", "#" + id);
            link.addClass("header-anchor");
            el.append(link)
        }
    });

    wrapLastCodeRayLineElement();

});
